#!/usr/bin/env bash

# Read-only utility script to check for leftover binary files from past installations.
# This might be helpful to narrow down possible causes of the error 31-4302.
#
# Usage: ./find_surplus_finaries.sh
#    Execute the script within the game installation directory.

per_line_data=$(jq -r "[.remoteName] | join(\"\")" "pkg_version")
pefiles=$(find . \( -iname '*.exe' -o -iname '*.sys' -o -iname '*.dll' \))

count_total=0
count_bad=0
while read -r filename; do
	filename=${filename##./}
	if [[ ! "$per_line_data" == *"$filename"* ]]; then
		echo "Culprit: $filename"
		count_bad=$((count_bad + 1))
	fi
	count_total=$((count_total + 1))
done <<< "$pefiles"

echo "Done. Checked ${count_total} files. ${count_bad} bad file(s)."
